//
//  Login&RegViewController.m
//  SeaChat
//
//  Created by 刘汉 on 16/6/14.
//  Copyright © 2016年 潘强. All rights reserved.
//

#import "Login&RegViewController.h"
#import "TalkViewController.h"
@interface Login_RegViewController ()<EMChatManagerDelegate,UITextFieldDelegate>

@property(nonatomic,strong)UITextField *userNameText;
@property(nonatomic,strong)UITextField *PassWordText;


@property(nonatomic,strong)UIButton *loginBtn;
@property(nonatomic,strong)UIButton *registerBtn;


@end

@implementation Login_RegViewController
- (void)dealloc
{
    [_userNameText release];
    [_PassWordText release];
    [_loginBtn release];
    [_registerBtn release];
    Block_release(_backToMainVC);
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationController.navigationBar.hidden =YES;

    self.view.backgroundColor = [UIColor whiteColor];
    self.userNameText = [UITextField new];
    self.userNameText.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.userNameText];
    [self.userNameText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(HEIGHT/4);
        make.left.mas_equalTo(WIDTH/4);
        make.right.mas_equalTo(-WIDTH/4);
        make.height.mas_equalTo(30);
    }];
    _userNameText.delegate = self;
    [self.userNameText release];


    self.PassWordText = [UITextField new];
    self.PassWordText.backgroundColor =[UIColor grayColor];
    _PassWordText.delegate =self;
    [self.view addSubview:self.PassWordText];
    [self.PassWordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.userNameText.bottom).offset(30);
        make.height.equalTo(self.userNameText);
        make.left.right.equalTo(self.userNameText);
    }];

    [self.PassWordText release];




    UILabel *namelabel = [UILabel new];
                      //alloc]initWithFrame:CGRectMake(WIDTH / 8  , HEIGHT / 4, WIDTH/5, 30)];
    [self.view addSubview:namelabel];

   [namelabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.mas_equalTo(HEIGHT/4);
    make.left.mas_equalTo(WIDTH/8);
    make.right.mas_equalTo(-WIDTH/5 * 2 );
    make.height.mas_equalTo(30);
    }];

    namelabel.text = @"用户名";
    [namelabel release];

    UILabel *pasWd =[UILabel new];
                      //alloc]initWithFrame:CGRectMake(WIDTH / 8  , HEIGHT / 4 + 60, WIDTH/5, 30)];

    [self.view addSubview:pasWd];
  [pasWd mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.mas_equalTo(namelabel.bottom).offset(30);
    make.height.equalTo(namelabel);
    make.left.right.equalTo(namelabel);
  }];
    pasWd.text = @"密码";
    [pasWd release];


    self.loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_loginBtn addTarget:self action:@selector(loginBtnAction) forControlEvents:UIControlEventTouchUpInside];

    [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [_loginBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [self.view addSubview:self.loginBtn];
[self.loginBtn makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(80);
    make.right.equalTo( - WIDTH/2);
    make.height.equalTo(40);
    make.top.equalTo(HEIGHT/2);

}];


    self.registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [_registerBtn setTintColor:[UIColor blackColor]];
    [_registerBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_registerBtn addTarget:self action:@selector(registerBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.registerBtn];
    [self.registerBtn makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.loginBtn.right).offset(-50);
        make.right.equalTo( -80 );
        make.height.equalTo(self.loginBtn);
        make.top.equalTo(HEIGHT/2);
        
    }];

    NSLog(@"%d",[EMClient sharedClient].isLoggedIn);


    [[EMClient sharedClient].options setIsAutoLogin:YES];

}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];
    return YES;
}
- (void)loginBtnAction{

    if (self.userNameText.text.length == 0 || self.PassWordText.text.length == 0) {

        NSLog(@"请输入账号密码")

    } else {

        EMError *error = [[EMClient sharedClient] loginWithUsername:self.userNameText.text password:self.PassWordText.text];

        if (!error) {

            NSLog(@"登录成功");
            if (self.view.window.rootViewController != self) {

                [[EMClient sharedClient].options setIsAutoLogin:YES];
                //登录成功回到主界面
                [self dismissViewControllerAnimated:1 completion:^{
                    
                    
                }];

            } else if (self.view.window.rootViewController == self) {
                self.backToMainVC();

            }
            
            
        }else{
            NSLog(@"%@",error);
        }
        

    }


}


-(void)registerBtnAction{
    EMError *error = [[EMClient sharedClient] registerWithUsername:self.userNameText.text password:self.PassWordText.text];
    if (error==nil) {
        NSLog(@"注册成功");
    }else{
        NSLog(@"%@",error);

    }


}
   @end
